package com.audi.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.audi.R;
import com.audi.view.DeviceUpdatePhoto;

public class ActivityReviewImageCrop extends Activity {
    DeviceUpdatePhoto deviceUpdatePhoto;
    ImageView imageView;
    Activity activity;
    String filePath = Environment.getExternalStorageDirectory()
	    + "/temporary_holder.jpg";
    int req, temp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);

	setContentView(R.layout.activity_review_image);
	ActionBar actionBar = getActionBar();
	actionBar.setTitle("Review Image Crop");

	deviceUpdatePhoto = new DeviceUpdatePhoto(this);
	Intent in = getIntent();
	Bundle b = in.getExtras();
	req = b.getInt("Req");
	temp = 0;
	imageView = (ImageView) findViewById(R.id.review_img);

	imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
	activity = this;
	Button ok = (Button) findViewById(R.id.buttonok);
	ok.setOnClickListener(new View.OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		// TODO Auto-generated method stub

		
		Intent intent=new Intent();  
        intent.putExtra("MESSAGE","close dialog");  
          Log.d("=========>>>>", "close dialog");
        setResult(555,intent); 
        activity.finish();
		//deviceUpdatePhoto.dimissUpdate();
	    }
	});
	Button cancel = (Button) findViewById(R.id.buttoncancel);
	cancel.setOnClickListener(new View.OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		// TODO Auto-generated method stub
	
	    	try {
	    		deviceUpdatePhoto.performcrop(deviceUpdatePhoto.getPicUri());
			} catch (Exception e) {
				// TODO: handle exception
				Log.e("============error", e+ "====6767");
			}
		    
		
	    }
	});
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

	super.onActivityResult(requestCode, resultCode, data);
	Intent myIntent = new Intent(this, ActivityReviewImageCrop.class);
	if (resultCode == RESULT_OK) {
	    if (requestCode == deviceUpdatePhoto.cameraCapture) {
		deviceUpdatePhoto.performcrop(deviceUpdatePhoto.getPicUri());
		temp = 1;
	    } else if (resultCode == deviceUpdatePhoto.picCrop) {
		String filePath = Environment.getExternalStorageDirectory()
			+ "/temporary_holder.jpg";
		temp = 1;
		myIntent.putExtra("Req", deviceUpdatePhoto.getstatus());
		this.startActivity(myIntent);
		// Toast.makeText(this, "Sssss 1", Toast.LENGTH_SHORT).show();
	    }

	    if (requestCode == deviceUpdatePhoto.pickFromFile) {
		temp = 1;
		deviceUpdatePhoto.setPicUri(data.getData());
		deviceUpdatePhoto.performcrop(deviceUpdatePhoto.getPicUri());
		// myIntent.putExtra("Req", 2);
		String filePath = Environment.getExternalStorageDirectory()
			+ "/temporary_holder.jpg";
		// Toast.makeText(this, "Sssss 2", Toast.LENGTH_SHORT).show();
	    }
	    imageView = (ImageView) findViewById(R.id.review_img);
	    imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));

	} else {

	    // Toast.makeText(activity, "sssss", Toast.LENGTH_LONG).show();
	    if (req == 1 && temp == 0) {
		//
		deviceUpdatePhoto.camphoto();
		temp = 1;
		this.finish();
		// Toast.makeText(activity, "temp 0", Toast.LENGTH_LONG).show();
	    } else if (req == 2) {
		deviceUpdatePhoto.picphoto();
		temp = 1;
		this.finish();
	    }

	    // else if(requestCode == deviceUpdatePhoto.picCrop){
	    // String filePath = Environment.getExternalStorageDirectory()
	    // + "/temporary_holder.jpg";
	    //
	    // Intent myIntent = new Intent(this,
	    // ActivityReviewImageCrop.class);
	    // this.startActivity(myIntent);
	    // }
	    // if (requestCode == deviceUpdatePhoto.pickFromFile) {
	    // deviceUpdatePhoto.setPicUri(data.getData());
	    // deviceUpdatePhoto.performcrop(deviceUpdatePhoto.getPicUri());
	    //
	    // String filePath = Environment.getExternalStorageDirectory()
	    // + "/temporary_holder.jpg";
	    //
	    // }
	}

    }

    // public static int cameraCapture = 1;
    // public static int picCrop = 2;
    // public static int pickFromFile = 3;
}
